package com.rq.demo.controller;

import android.os.Bundle;
import android.view.View;

import com.rq.base.ui.BaseController;
import com.rq.base.ui.impl.OnClick;
import com.rq.demo.R;
import com.rq.rvlibrary.BaseAdapter;
import com.rq.rvlibrary.EasyViewHolder;
import com.rq.rvlibrary.NestingViewHolder;
import com.rq.rvlibrary.RecyclerUtil;

import java.util.ArrayList;
import java.util.List;

public class RecyclerController extends BaseController implements View.OnClickListener {
    @Override
    public int getLayoutId() {
        return R.layout.act_recycler;
    }

    BaseAdapter mAdapter;

    @Override
    public void onViewCreated(Bundle savedInstanceState) {
        super.onViewCreated(savedInstanceState);
        mAdapter = new BaseAdapter(getActivity(), R.layout.item_parent, DRVH.class, this);
        setData2View(R.id.recycler, mAdapter);
    }

    public static class DRVH extends NestingViewHolder {
        public DRVH(View itemView) {
            super(itemView);
        }

        @Override
        public int inflateLayoutId() {
            return R.layout.item_parent;
        }

        @Override
        protected void fillChildData(EasyViewHolder holder, int position, Object childBean, int pPostion) {
            super.fillChildData(holder, position, childBean, pPostion);
            DebugData item = (DebugData) childBean;
            holder.setItemText(R.id.text_time, item.time);
            holder.setItemText(R.id.text_ml, item.num);
        }

        @Override
        protected List getChildData(Object parentBean, int position) {
            return (List) parentBean;
        }

        @Override
        protected RecyclerUtil getChildRecyclerUtil(BaseAdapter adapter) {
            return new RecyclerUtil(adapter).horizontal();
        }

        @Override
        protected int getChildLayoutId() {
            return R.layout.item_child;
        }

        @Override
        protected int getChildRecyclerId() {
            return R.id.recycler_inner;
        }
    }

    public class DebugData implements NestingViewHolder.Extendable {
        public DebugData(int i, int j) {
            this.time = i + ".time" + j;
            this.num = i + ".num" + j;
        }

        String time;
        String num;

        @Override
        public boolean isOpen() {
            return true;
        }
    }

    @OnClick({R.id.btn_switch})
    public void onClick(View v) {
        if (v.getId() == R.id.btn_switch) {
            List<List<DebugData>> lists = new ArrayList<>();
            for (int i = 0; i < 12; i++) {
                List<DebugData> data = new ArrayList<>();
                for (int j = 0; j < 4; j++) {
                    data.add(new DebugData(i, j));
                }
                lists.add(data);
            }
            mAdapter.setData(lists);

        }
    }
}
